Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Breaking API Change: drivers: can: rework support for manual bus-off recovery #69460

Conversation

henrikbrixandersen
Copy link
Member

Since all CAN controllers drivers seem to support automatic recovery (for any future drivers for hardware without this hardware capability this can easily be implemented in the driver), change the Zephyr CAN controller API policy to:

  • Always enable automatic bus recovery upon driver initialization, regardless of Kconfig options. Since CAN controllers are initialized in "stopped" state, no unwanted bus-off recovery will be started at this point.

  • Invert and rename the Kconfig CONFIG_CAN_AUTO_BUS_OFF_RECOVERY, which is enabled by default, to CONFIG_CAN_MANUAL_RECOVERY_MODE, which is disabled by default. Enabling CONFIG_CAN_MANUAL_RECOVERY_MODE=y enables support for the can_recover() API function and a new manual recovery mode (see next bullet). Keeping this guarded by Kconfig allows keeping the flash footprint down for applications not using manual bus-off recovery.

  • Introduce a new CAN controller operational mode CAN_MODE_MANUAL_RECOVERY. Support for this is only enabled if CONFIG_CAN_MANUAL_RECOVERY_MODE=y. Having this as a mode allows applications to inquire whether the CAN controller supports manual recovery mode via the can_get_capabilities() API function and either fail or rely on automatic recovery - and it allows CAN controller drivers not supporting manual recovery mode to fail early in can_set_mode() during application startup instead of failing when can_recover() is called at a later point in time.

RFC: #69459

@henrikbrixandersen henrikbrixandersen added RFC Request For Comments: want input from the community area: CAN labels Feb 26, 2024
@henrikbrixandersen henrikbrixandersen self-assigned this Feb 26, 2024
@henrikbrixandersen henrikbrixandersen added this to the v3.7.0 milestone Feb 26, 2024
@henrikbrixandersen henrikbrixandersen force-pushed the can_manual_vs_auto_recover_mode branch 4 times, most recently from 933a96b to 629a39b Compare February 26, 2024 20:36
@henrikbrixandersen henrikbrixandersen force-pushed the can_manual_vs_auto_recover_mode branch from 629a39b to 453e4d9 Compare February 26, 2024 20:56
@henrikbrixandersen henrikbrixandersen added the Release Notes Required Release notes required for this change label Feb 27, 2024
@henrikbrixandersen henrikbrixandersen changed the title RFC: drivers: can: rework support for manual bus-off recovery RFC: Breaking API Change: drivers: can: rework support for manual bus-off recovery Feb 27, 2024
@henrikbrixandersen henrikbrixandersen added the Breaking API Change Breaking changes to stable, public APIs label Feb 27, 2024
@henrikbrixandersen henrikbrixandersen force-pushed the can_manual_vs_auto_recover_mode branch 3 times, most recently from 36cc408 to d4e44a6 Compare February 28, 2024 21:50
@henrikbrixandersen henrikbrixandersen removed the Release Notes Required Release notes required for this change label Feb 28, 2024
@henrikbrixandersen henrikbrixandersen marked this pull request as ready for review February 28, 2024 21:50
@henrikbrixandersen henrikbrixandersen added the Release Notes To be mentioned in the release notes label Feb 28, 2024
@henrikbrixandersen henrikbrixandersen force-pushed the can_manual_vs_auto_recover_mode branch 2 times, most recently from e645eb0 to 8992d05 Compare February 28, 2024 23:22
@henrikbrixandersen
Copy link
Member Author

RFC ready for review.

martinjaeger
martinjaeger previously approved these changes Feb 29, 2024
Copy link
Member

@martinjaeger martinjaeger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Only got one minor non-blocking comment which may be applied to some other drivers as well.

Comment on lines +357 to +369
can_mode_t supported = CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY;
struct can_mcan_data *data = dev->data;
uint32_t cccr;
uint32_t test;
int err;

#ifdef CONFIG_CAN_FD_MODE
if ((mode & ~(CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY | CAN_MODE_FD)) != 0U) {
LOG_ERR("unsupported mode: 0x%08x", mode);
return -ENOTSUP;
if (IS_ENABLED(CONFIG_CAN_MANUAL_RECOVERY_MODE)) {
supported |= CAN_MODE_MANUAL_RECOVERY;
}
#else /* CONFIG_CAN_FD_MODE */
if ((mode & ~(CAN_MODE_LOOPBACK | CAN_MODE_LISTENONLY)) != 0U) {

if (IS_ENABLED(CONFIG_CAN_FD_MODE)) {
supported |= CAN_MODE_FD;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call can_mcan_get_capabilities here in order to avoid code duplication?

Copy link
Member

@manuargue manuargue Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it seems that all drivers can benefit from this and avoid issues like #69460 (comment) :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, yes - I too noticed that pattern when converting the drivers. I didn't want to mix that change into this PR, but I'm preparing to move this generic check into the can_set_mode() implementation function itself.

dleach02
dleach02 previously approved these changes Feb 29, 2024
@congnguyenhuu
Copy link
Contributor

Look good to me.
But I see that nxp s32 canxl driver is missing capability for manual recovery mode. Please update this and check this in other drivers.@henrikbrixandersen

static int can_nxp_s32_get_capabilities(const struct device *dev, can_mode_t *cap)

Copy link
Member

@manuargue manuargue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing capability for manual recovery mode in NXP S32 CAXL driver as pointed out above

@henrikbrixandersen henrikbrixandersen dismissed stale reviews from dleach02 and martinjaeger via f5b8907 March 1, 2024 08:16
@henrikbrixandersen
Copy link
Member Author

Look good to me. But I see that nxp s32 canxl driver is missing capability for manual recovery mode. Please update this and check this in other drivers.@henrikbrixandersen

Nicely spotted, thanks. I have updated the patch.

@henrikbrixandersen henrikbrixandersen force-pushed the can_manual_vs_auto_recover_mode branch from f5b8907 to 71f1775 Compare March 1, 2024 08:21
Since all CAN controllers drivers seem to support automatic recovery (for
any future drivers for hardware without this hardware capability this can
easily be implemented in the driver), change the Zephyr CAN controller API
policy to:

- Always enable automatic bus recovery upon driver initialization,
  regardless of Kconfig options. Since CAN controllers are initialized in
  "stopped" state, no unwanted bus-off recovery will be started at this
  point.

- Invert and rename the Kconfig CONFIG_CAN_AUTO_BUS_OFF_RECOVERY, which is
  enabled by default, to CONFIG_CAN_MANUAL_RECOVERY_MODE, which is disabled
  by default. Enabling CONFIG_CAN_MANUAL_RECOVERY_MODE=y enables support
  for the can_recover() API function and a new manual recovery mode (see
  next bullet). Keeping this guarded by Kconfig allows keeping the flash
  footprint down for applications not using manual bus-off recovery.

- Introduce a new CAN controller operational mode
  CAN_MODE_MANUAL_RECOVERY. Support for this is only enabled if
  CONFIG_CAN_MANUAL_RECOVERY_MODE=y. Having this as a mode allows
  applications to inquire whether the CAN controller supports manual
  recovery mode via the can_get_capabilities() API function and either fail
  or rely on automatic recovery - and it allows CAN controller drivers not
  supporting manual recovery mode to fail early in can_set_mode() during
  application startup instead of failing when can_recover() is called at a
  later point in time.

Signed-off-by: Henrik Brix Andersen <[email protected]>
List the changes to the CAN bus-off recovery functionality.

Signed-off-by: Henrik Brix Andersen <[email protected]>
@henrikbrixandersen henrikbrixandersen force-pushed the can_manual_vs_auto_recover_mode branch from 71f1775 to 9426740 Compare March 1, 2024 08:25
@henrikbrixandersen
Copy link
Member Author

Rebased on main to resolve merge conflicts in doc/releases/migration-guide-3.7.rst and drivers/can/can_stm32_bxcan.c.

@aescolar aescolar merged commit 418b0a1 into zephyrproject-rtos:main Mar 2, 2024
22 checks passed
@henrikbrixandersen henrikbrixandersen deleted the can_manual_vs_auto_recover_mode branch March 4, 2024 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: CAN Breaking API Change Breaking changes to stable, public APIs Release Notes To be mentioned in the release notes RFC Request For Comments: want input from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RFC: Breaking API Change: drivers: can: rework support for manual bus-off recovery
6 participants